package com.yunxin.core.domain.service.merchant.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.yunxin.core.domain.external.product.ProductExternalService;
import com.yunxin.core.domain.model.merchant.*;
import com.yunxin.core.domain.service.merchant.MerchantService;
import com.yunxin.core.infra.common.merchant.enums.MerchantStatusEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author zero°
 * @since 2023-05-27
 */
@DS("core")
@Service
@RequiredArgsConstructor
public class MerchantServiceImpl implements MerchantService {

    private final MerchantInfoRepository merchantInfoRepository;

    private final MerchantContactPersonRepository merchantContactPersonRepository;

    private final MerchantInvoiceInfoRepository merchantInvoiceInfoRepository;

    private final ProductExternalService productExternalService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addMerchantData(MerchantInfo merchantInfo, MerchantContactPerson merchantContactPerson, MerchantInvoiceInfo merchantInvoiceInfo) {
        boolean result = merchantInfoRepository.saveMerchantInfo(merchantInfo);
        merchantContactPerson.setMerchantId(merchantInfo.getId());
        merchantContactPersonRepository.saveMerchantContactPerson(merchantContactPerson);
        merchantInvoiceInfo.setMerchantId(merchantInfo.getId());
        merchantInvoiceInfoRepository.saveMerchantContactPerson(merchantInvoiceInfo);
        return result;
    }

    @Override
    public boolean updateMerchantData(MerchantInfo merchantInfo, MerchantContactPerson merchantContactPerson, MerchantInvoiceInfo merchantInvoiceInfo) {
        // 更新商户信息
        boolean result = merchantInfoRepository.updateMerchantInfo(merchantInfo);
        //逻辑删除商户联系人
        boolean result1 = merchantContactPersonRepository.updateMerchantContactDelPersonByMerchantId(merchantContactPerson.getCreator(), merchantContactPerson.getMerchantId());
        // 逻辑删除商户发票信息
        merchantInvoiceInfoRepository.updateMerchantInvoiceInfoDelPersonByMerchantId(merchantInvoiceInfo.getCreator(), merchantInvoiceInfo.getMerchantId());
        // 新增商户联系人
        merchantContactPersonRepository.saveMerchantContactPerson(merchantContactPerson);
        // 新增商户发票信息
        merchantInvoiceInfoRepository.saveMerchantContactPerson(merchantInvoiceInfo);
        return result || result1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateMerchantStatus(MerchantInfo oldMerchantInfo, Integer merchantStatus, String editor) {
        MerchantInfo merchantInfo = new MerchantInfo();
        merchantInfo.setId(oldMerchantInfo.getId());
        merchantInfo.setMerchantStatus(merchantStatus);
        merchantInfo.setEditor(editor);
        merchantInfo.setVersion(oldMerchantInfo.getVersion() + 1);
        boolean result = merchantInfoRepository.updateMerchantInfo(merchantInfo);
        if (result && MerchantStatusEnum.PERSON.getValue().equals(merchantStatus)) {
            productExternalService.offShelfMerchantProduct(oldMerchantInfo.getId(), editor);
        }
        return result;
    }

    @Override
    public MerchantInfo selectByMerchantId(Long id) {

        return  merchantInfoRepository.getMerchantInfoById(id);
    }


}
